-
Notifications
You must be signed in to change notification settings - Fork 715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Random package not depend on global state #146
Conversation
@Stefan-Hanke @sladwig Can you take a look? |
src/core/reducer.js
Outdated
@@ -84,11 +86,12 @@ export function createGameReducer({ game, numPlayers, multiplayer }) { | |||
} | |||
|
|||
// Init PRNG state. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That comment is out of date.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
src/core/flow.js
Outdated
@@ -278,7 +278,8 @@ export function FlowWithPhases({ | |||
const startTurn = function(state, config) { | |||
const ctx = { ...state.ctx }; | |||
const G = config.onTurnBegin(state.G, ctx); | |||
const _undo = [{ G, ctx }]; | |||
const { random, ...ctxWithoutAPI } = ctx; // eslint-disable-line no-unused-vars |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to hide how random
is attached to ctx
. How about a function inside random.js that performs that action?
How does this scale to, say, the Events API? Does this have to be attached/stripped likewise?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. The Events API will probably have a similar attach / detach
mechanism for ctx.
Looks ok to me. I had expected you'd need to change the JSON inside index.test.js also, but then, why should it change; the |
Looks goot to me too, will test it later in game. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just checked in game and works like a charm :)
* remove global state * update docs * remove from package * update docs * update docs * review comments * update docs/react/boardgameio.min.js
We have the following problem at the moment:
The
Random
package is bundled intoreact.js
,core.js
andclient.js
. When the user calls the API, they use the version incore.js
. However, when the framework tries to persist the PRNG state, it uses the version inreact.js
(if using a React client). This loses the "global" state, and makes the Random package always generate the same numbers, regardless of which seed is used.Fix
This PR injects the Random API into
ctx
instead, so you would use it like:This also has the nice property that the individual move sub-reducers are now pure.
Checklist
master
).